﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Share.Utilities;
using System.Linq;


using Share.Data;
using System.IO;
namespace Client
{
    public static class RegisterHelper
    {
        public static bool CheckExistBD()
        {
            using (var dc = ShareDataCache.GetDataClientDataContext())
            {
                var client = dc.Clients.FirstOrDefault(c => c.ClientCode == SysInfo.HDDNumID && c.IsDeleted == false);
                return client != null;
            }

        }
        static string sFile = System.IO.Path.GetTempPath() + "ViCodeLeVi.IsActive";
        public static bool CheckIsActiveOnFile()
        {            
            return File.Exists(sFile);
        }
        private static void SaveFileActive()
        {
            if (!File.Exists(sFile))
                File.CreateText(sFile);
        }
        public static bool CheckIsActive()
        {
            try
            {
                using (var dc = ShareDataCache.GetDataClientDataContext())
                {
                    var client = dc.Clients.FirstOrDefault(c => c.ClientCode == SysInfo.HDDNumID && c.IsDeleted == false && c.IsActive == true);
                    return client != null;
                }
            }
            catch
            {
                return CheckIsActiveOnFile();
            }

        }

        public static bool CheckIsAgreeRegister()
        {
            using (var dc = ShareDataCache.GetDataClientDataContext())
            {
                var client = dc.Clients.FirstOrDefault(c => c.ClientCode == SysInfo.HDDNumID && c.IsDeleted == false && c.IsAgreeRegister == true);
                return client != null;
            }

        }


        public static void InsertClient()
        {
            try
            {
                if (CheckExistBD() == false)
                {
                    using (var dc = ShareDataCache.GetDataClientDataContext())
                    {
                        Share.Data.Client client = new Share.Data.Client();
                        client.IsActive = false;
                        client.IsDeleted = false;
                        client.IsAgreeRegister = false;
                        client.IPAddress = SysInfo.IpAddress;
                        client.ComputerName = SysInfo.ComputerName;
                        client.CreateDate = DateTime.Now;
                        client.ClientCode = SysInfo.HDDNumID;
                        client.SyncCode = string.Empty;
                        dc.Clients.InsertOnSubmit(client);
                        dc.SubmitChanges();
                        //MessageBox.Show("Insert client success");

                    }

                }

                using (var dc = ShareDataCache.GetDataClientDataContext())
                {
                    var client = dc.Clients.FirstOrDefault(c => c.IsDeleted == false && c.ClientCode == SysInfo.HDDNumID);
                    if (client != null)
                    {
                        ClientLogin _clientLogin = new ClientLogin();
                        _clientLogin.ClientCode = SysInfo.HDDNumID;
                        _clientLogin.ClientCodeOfClient = client.ClientCode;
                        _clientLogin.ClientId = client.ClientId;
                        _clientLogin.ComputerName = client.ComputerName;
                        _clientLogin.IsDeleted = false;

                        _clientLogin.LoginDate = DateTime.Now;
                        _clientLogin.SyncCode = string.Empty;
                        dc.ClientLogins.InsertOnSubmit(_clientLogin);
                        dc.SubmitChanges();

                    }
                }
                if (CheckIsAgreeRegister() == false)
                {
                    if (MessageBox.Show("You have not registered. Do you want register?", "Register", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        ThreadHelper.Runner.UIThreadCall(() =>
                            {
                                WinRegister win = new WinRegister();
                                win.ShowDialog();
                                var _client = win.Client_Register;
                                if (string.IsNullOrEmpty(win.CheckInvalid()))
                                {
                                    using (var dc = ShareDataCache.GetDataClientDataContext())
                                    {
                                        var client = dc.Clients.FirstOrDefault(c => c.IsDeleted == false && c.ClientCode == SysInfo.HDDNumID);
                                        if (client != null)
                                        {
                                            client.IsAgreeRegister = true;
                                            client.SupplierAddress = _client.SupplierAddress;
                                            client.SupplierEmail = _client.SupplierEmail;
                                            client.SupplierName = _client.SupplierName;
                                            client.SupplierPhone = _client.SupplierPhone;
                                            dc.SubmitChanges();
                                            MessageBox.Show("Register success");
                                        }
                                    }
                                }
                                else
                                {
                                    KILL();
                                    return;
                                }
                            });
                    }
                    else
                    {
                        return;
                    }
                }
                if (CheckIsActive() == false)
                {
                    MessageBox.Show("Client has not been activated");
                    KILL();
                }
                else
                {
                    SaveFileActive();
                }
            }

            catch (Exception ex)
            {
                if (!CheckIsActiveOnFile())
                {
                    MessageBox.Show("Connect to Server fail\nPlease ensure that your computer is connected to the internet.\n" + ex.Message);
                    KILL();
                }
            }
        }
        private static void  KILL()
        {
            Application.Current.Shutdown();
        }
    }
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class WinMainClient : Window
    {
        public WinMainClient()
        {
            InitializeComponent();
            ThreadHelper.Runner.RunInBackground(() =>
                {
                    RegisterHelper.InsertClient();
                });
        }
       
    }
}
